/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.datastructure;

import edu.yonsei.iwb.sdt.SDTLearner;
import edu.yonsei.iwb.sdt.SDTLearningType;
import edu.yonsei.iwb.sdt.datastructure.tree.DNode;
import java.util.NoSuchElementException;
import java.util.Vector;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;
import edu.yonsei.iwb.sdt.refinement.RefinementType;
import edu.yonsei.iwb.sdt.util.UtilFunctions;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 *
 * @author JDK
 */
public class DecomposeCandidateSet extends Vector<DecomposeCandidate> {

//    Resource centerClass;
    private Vector<String> targetclass;
    private Vector<PropertyArc> histPropertySet;
    private Vector<PropertyArc> connectedPropertySet;
    private ArrayList<String> lastResourceSet;

    public DecomposeCandidateSet() {
        this.targetclass = new Vector<String>();        
    }    

    public void setTargetclass(String targetclass) {
        this.targetclass.add(targetclass);
    }

    public String getLastAddedTargetClass() {
        try {
            return this.targetclass.lastElement();
        } catch (NullPointerException npe) {
            return null;
        } catch (NoSuchElementException nsee){
            return null;
        }
    }

    public void setPropertySet(PropertyArc p) {
        if (histPropertySet == null) {
            this.histPropertySet = new Vector<PropertyArc>();
            this.histPropertySet.add(p);
        } else {
            this.histPropertySet.add(p);
        }
    }

    public void setHistoryPropertySet(Vector<PropertyArc> propertySet) {
        try {
            if (propertySet.isEmpty()) {
                this.histPropertySet = new Vector<PropertyArc>();
            } else {
                this.histPropertySet = new Vector<PropertyArc>();
                for (int i = 0; i < propertySet.size(); i++) {
                    this.histPropertySet.add(propertySet.get(i));

                }
            }
        } catch (NullPointerException npe) {
        }
    }

    public Vector<PropertyArc> getHistoryPropertySet() {
        return this.histPropertySet;
    }
    
    public void setLastPositionedResourceSet(ArrayList<String> lrs){
        this.lastResourceSet = lrs;
    }
    
    public ArrayList<String> getLastPositionedResourceSet(){
        return this.lastResourceSet;
    }
    
    public void setConnectedPropertySet(Vector<PropertyArc> paset){
        this.connectedPropertySet = paset;
    }
    
    public Vector<PropertyArc> getConnectedPropertySet(){
        return this.connectedPropertySet;
    }

//    public void setCenterClass(Resource centerClass) {
//        this.centerClass = centerClass;
//    }
//
//    public Resource getCenterClass() {
//        return this.centerClass;
//    }

    public void calculateInformationGain(DNode parentNode, TargetInstanceSet tis) {
        double pnodeEntropy = UtilFunctions.calculateEntropy(parentNode.getTargetInstanceValueInformation(), parentNode.getInstanceSetSize());
        int pnodeInsSize = parentNode.getInstanceSetSize();
        ArrayList<DecomposeCandidate> zeroSet = new ArrayList();
        for (int i = 0; i < this.size(); i++) {
            DecomposeCandidate dc = this.get(i);
            Vector<DNode> childNodeSet = dc.getChildNodeSet();
            double sumofEntropy = 0.0;
//            dc.printDecomposeCandidateDetail();
//            System.out.println("***************************************  child size - "+childNodeSet.size());
            for (int j = 0; j < childNodeSet.size(); j++) {
                DNode childnode = childNodeSet.get(j);
                childnode.setTargetValueInstanceInformation(tis);
                
//                System.out.println("#--- " + childnode.getRefinementName());                                
                childnode.setEntropy();
//                System.out.println(childnode.toString());
                double cnodeEntropy = childnode.getEntropy();
                double childnodesize = childnode.getInstanceSetSize();
                double outofsize = childnodesize / pnodeInsSize;
//                System.out.println("----------  " + outofsize);
                sumofEntropy = sumofEntropy + outofsize * cnodeEntropy;

            }

            double n = pnodeEntropy - sumofEntropy;
//            System.out.println("information gain = pnodeE: " +pnodeEntropy+" - cnodeE: "+sumofEntropy+" = "+n);
            if(n == 0){
//                if(dc.getRefinementType() > RefinementType.IS_EXPANDED){
                    zeroSet.add(dc);
//                }
            }
            dc.setInformation_Gain(n);
//            dc.printDecomposeCandidateDetail();
        }
        
        for (int i = 0; i < zeroSet.size(); i++) {
            DecomposeCandidate dc = zeroSet.get(i);
            this.remove(dc);            
        }        
    }

    public void printDecomposeCandidateSet() {
        for (int i = 0; i < this.size(); i++) {
            DecomposeCandidate dc = this.get(i);
            dc.printDecomposeCandidateDetail();
        }
    }    
}
